@model MvcWeb.Models.BlogArchive
@using System.Globalization
@using Piranha
@{
    ViewBag.Title = Model.Title;

    Func<string> BlogLink = () => {
        return Model.Permalink
            + (Model.Archive.Category != null ? $"/category/{Model.Archive.Category.Slug}"  : "")
            + (Model.Archive.Year.HasValue ? $"/{Model.Archive.Year}" : "" )
            + (Model.Archive.Month.HasValue ? $"/{Model.Archive.Month}" : "");
    };

    Func<string> MonthName = () => {
        if (Model.Archive.Month.HasValue) {
            return new DateTime(2018, Model.Archive.Month.Value, 1) .ToString("MMMM", CultureInfo.InvariantCulture);
        }
        return "";
    };

    Func<Piranha.Models.DynamicPost, Piranha.Extend.Blocks.HtmlBlock> GetFirstHtmlBlock = (post) => 
    {
        foreach (var block in post.Blocks)
        {
            if (block is Piranha.Extend.Blocks.HtmlBlock)
            {
                return (Piranha.Extend.Blocks.HtmlBlock)block;
            }
        }
        return null;
    };
}

@Html.DisplayFor(m => m.Hero)

<div class="container body-container">
    <div class="row justify-content-center">
        <div class="col-lg-8 col-md-10 col-xs-12">
            @foreach (var post in Model.Archive.Posts) 
            {
                <article class="archive-item">
                    <header>
                        <h2><a href="@post.Permalink">@post.Title</a></h2>
                    </header>
                    <p class="small">
                        <strong>In</strong> <a href="/blog/category/@post.Category.Slug">@post.Category.Title</a> <span class="sep">&#9670;</span>
                        <strong>Tags</strong>
                        @foreach (var tag in post.Tags)
                        {
                            <a href="/blog/tag/@tag.Slug">#@tag.Title</a>
                        }
                        <span class="sep">&#9670;</span>
                        <strong>Published</strong> @post.Published.Value.ToString("yyyy-MM-dd")
                    </p>
                    @if (post.Regions.Hero.PrimaryImage.Media != null)
                    {
                        <img src="@Url.Content(post.Regions.Hero.PrimaryImage.Media.PublicUrl)">
                    }
                    @Html.Raw(GetFirstHtmlBlock(post)?.Body ?? "")

                    <a href="@post.Permalink" class="btn btn-sm btn-default">Read more</a>
                </article>
            }
        </div>    
    </div>

    @if (Model.Archive.TotalPages > 1) 
    {
        <div class="row">
            <div class="col">
                <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    <li class="page-item">
                        <a class="page-link" href="@BlogLink()/page/1">
                            <span aria-hidden="true">&laquo;</span>
                            <span class="sr-only">Previous</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="@BlogLink()/page/@Math.Max(1, Model.Archive.CurrentPage - 1)">
                            <span aria-hidden="true">&lsaquo;</span>
                            <span class="sr-only">Previous</span>
                        </a>
                    </li>
                    @for (var n = 1; n <= Model.Archive.TotalPages; n++) 
                    {
                        <li class="page-item @(Model.Archive.CurrentPage == n ? "active" : "")"><a class="page-link" href="@BlogLink()/page/@n">@n</a></li>
                    }
                    <li class="page-item">
                        <a class="page-link" href="@BlogLink()/page/@Math.Min(Model.Archive.TotalPages, Model.Archive.CurrentPage + 1)">
                            <span aria-hidden="true">&rsaquo;</span>
                            <span class="sr-only">Next</span>                        
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="@BlogLink()/page/@Model.Archive.TotalPages">
                            <span aria-hidden="true">&raquo;</span>
                            <span class="sr-only">Next</span>                        
                        </a>
                    </li>
                </ul>
                </nav>
            </div>
        </div>        
    }
</div>
